package org.grow.common.exception;

import io.jsonwebtoken.ExpiredJwtException;
import io.jsonwebtoken.JwtException;
import io.jsonwebtoken.security.SignatureException;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author: xwg
 * @CreateDate: 2023/9/20
 */
// 统一异常处理 能够允许后端controller随意抛出部分异常 转化成http响应给前端
// 统一异常处理 不仅处理自己定义的异常 也处理框架的异常 转化为http响应
@RestControllerAdvice
public class ExceptionBridge {
    @ExceptionHandler(BillStateException.class)
    public ResponseEntity<Map<String,Object>> bse(BillStateException e){
        HashMap<String, Object> map = new HashMap<>();
        map.put("msg",e.getMessage());
        map.put("code",888);
        return new ResponseEntity<>(map,null,288);
    }
    @ExceptionHandler(MissTargetException.class)
    public ResponseEntity<Map<String,Object>> mte(MissTargetException e){
        HashMap<String, Object> map = new HashMap<>();
        map.put("msg",e.getMessage());
        map.put("code",666);
        return new ResponseEntity<>(map,null,288);
    }
    @ExceptionHandler(NoAuthException.class)
    public ResponseEntity<Map<String,Object>> nae(NoAuthException e){
        HashMap<String, Object> map = new HashMap<>();
        map.put("msg",e.getMessage());
        map.put("code",401);
        return new ResponseEntity<>(map,null,401);
    }
    @ExceptionHandler(SignatureException.class)
    public ResponseEntity<Map<String,Object>> se(SignatureException e){
        HashMap<String, Object> map = new HashMap<>();
        map.put("msg",e.getMessage());
        map.put("code",27701);
        return new ResponseEntity<>(map,null,288);
    }
    @ExceptionHandler(ExpiredJwtException.class)
    public ResponseEntity<Map<String,Object>> eje(ExpiredJwtException e){
        HashMap<String, Object> map = new HashMap<>();
        map.put("msg",e.getMessage());
        map.put("code",27702);
        return new ResponseEntity<>(map,null,288);
    }
    @ExceptionHandler(JwtException.class)
    public ResponseEntity<Map<String,Object>> eje(JwtException e){
        HashMap<String, Object> map = new HashMap<>();
        map.put("msg",e.getMessage());
        map.put("code",27703);
        return new ResponseEntity<>(map,null,288);
    }
}
